package com.example.babyapp;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class PreviewActivity extends Activity{
	
	Button send;
	String strBabyName, strEmailSubj, strBabyGender, strBabyBirthday, strBirthPlace,
	strMother, strFather, strContactNo, strUser, strOverAllBody, strBirthWeight, strHereditary,
	strInfection, strDefect, strAddress, strStreet, strCity, strState, strZipcode;
	String resultR = " ";
	String resultL = " ";
	EditText emailAddress;
	
	public void onCreate(Bundle bundle){
		super.onCreate(bundle);
		setContentView(R.layout.activity_preview);
		setOrientation();
		
		Intent intent = getIntent();
		String[] strBabyInfo = intent.getStringArrayExtra("babyInfo");
		String[] strSecondInfo = intent.getStringArrayExtra("secondInfo");
		String[] strOtherInfo = intent.getStringArrayExtra("otherInfo");
		
		Intent intentTwo = getIntent();
		ArrayList<String> testRight = intentTwo.getStringArrayListExtra("testResultRight");
		ArrayList<String> testLeft = intentTwo.getStringArrayListExtra("testResultLeft");
		
		int iCountR = resultR.length(), j=0;
		int iCountL = resultL.length(), l=0;
		
		for(String s:testRight) {
			j++;
			for(int i =0; i < iCountR; i++) {
			resultR += "\nTrial Number " + j + ": \n" + s;
			}
		}
		
		for(String s:testLeft) {
			l++;
			for(int i =0; i < iCountL; i++) {
			resultL += "\nTrial Number " + l + ": \n" + s;
			}
		}
		
		strBabyName = strBabyInfo[0] + ", " + strBabyInfo[1];
		strBabyGender = strBabyInfo[2];
		strBabyBirthday = strBabyInfo[3];
		strBirthPlace = strBabyInfo[4];
		strBirthWeight = strBabyInfo[5];
		
		strMother = strSecondInfo[0];
		strFather = strSecondInfo[1];
		strContactNo = strSecondInfo[2];
		strStreet = strSecondInfo[3];
		strCity = strSecondInfo[4];
		strState = strSecondInfo[5];
		strZipcode = strSecondInfo[5];
		
		strAddress = strStreet + " " + strCity + " " + strState + " " + strZipcode;
		
		strHereditary  = strOtherInfo[0];
		strInfection = strOtherInfo[1];
		strDefect = strOtherInfo[2];
		strUser = "NIH User";
		
		TextView babyname = (TextView) findViewById(R.id.babynameTxt);
		babyname.setText(strBabyName);

		TextView babySex = (TextView) findViewById(R.id.sexTxt);
		babySex.setText(strBabyGender);

		TextView birthDate = (TextView) findViewById(R.id.dobTxt);
		birthDate.setText(strBabyBirthday);

		TextView birthPlace = (TextView) findViewById(R.id.pobTxt);
		birthPlace.setText(strBirthPlace);

		TextView birthWeight = (TextView) findViewById(R.id.weightTxt);
		birthWeight.setText(strBirthWeight);

		TextView mother = (TextView) findViewById(R.id.nomTxt);
		mother.setText(strMother);
		
		TextView father = (TextView) findViewById(R.id.nofTxt);
		father.setText(strFather);
		
		TextView address = (TextView) findViewById(R.id.addressTxt);
		address.setText(strAddress);
		
		TextView contact = (TextView) findViewById(R.id.contactTxt);
		contact.setText(strContactNo);
		
		TextView hereditary = (TextView) findViewById(R.id.hereditaryTxt);
		hereditary.setText("Answer: " + strHereditary);
		
		TextView infection = (TextView) findViewById(R.id.infectionTxt);
		infection.setText("Answer: " + strInfection);
		
		TextView defect = (TextView) findViewById(R.id.defectTxt);
		defect.setText("Answer: " + strDefect);
		
		TextView resultxtR = (TextView) findViewById(R.id.resultTxtR);
		resultxtR.setText(resultR);
		
		TextView resultxtL = (TextView) findViewById(R.id.resultTxtL);
		resultxtL.setText(resultL);
		
		emailAddress = (EditText) findViewById(R.id.emailAddField);
		
		SimpleDateFormat sdf = new SimpleDateFormat("MMM-dd-yyyy");
		String strDate = sdf.format(new Date());
		
		strEmailSubj = strBabyName + " " + strDate + " " + "Baby Hearing Test Result";
		
		strOverAllBody = "Baby Name: " + strBabyName + "\n Baby Gender: " + strBabyGender + "\n Baby Birthday: " + strBabyBirthday 
				+ "\nBirth Place: " + strBirthPlace + "\nMother: " + strMother + "\nFather: " + strFather 
				+ "\nBirthWeight: " + strBirthWeight +  "\nContact Number: " + strContactNo + "\nHereditary: " + strHereditary
				+ "\nInfection: " + strInfection + "\nDefect" + strDefect + "\nTester: " + strUser 
				+ "\n The right ear test result: \n" + resultR + "\n The left ear test result: \n" + resultL+ "\nTester/User: " + strUser;

        send = (Button) findViewById(R.id.sendBtn);
		send.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                sendEmail();
            }
        });
	}
	
	public void sendEmail(){
		 
        if(!emailAddress.getText().toString().trim().equalsIgnoreCase("")){
          final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
          emailIntent.setType("plain/text");
          emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{ emailAddress.getText().toString()});
          emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, strEmailSubj);
          emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, strOverAllBody);
          PreviewActivity.this.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
        }
        else{
            Toast.makeText(getApplicationContext(), "Please enter an email address..", Toast.LENGTH_LONG).show();
        }
      }
	
	protected void setOrientation() {
    	int current = getRequestedOrientation();
    	if(current != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
    		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    	}
    }

}
